﻿Imports System.Data.OleDb
Partial Public Class Discussions
    Inherits System.Web.UI.Page
    Dim UserID As Integer
    Dim UserImage As String
    Dim petID As Int16
    Public temp As Integer
    Dim countViews As Integer
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        UserName_LBL.Text = User.Identity.Name
        If Request.QueryString("ID") = Nothing Then
        Else
            Dim ID As String = Request.QueryString("ID").ToString()

            Dim con As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings _
                                                 ("PetsConnectionString").ToString())

            con.Open()


            Dim sqlString As String = "select * from PetDetails where ID=" + ID + ""
            Dim cmd As OleDb.OleDbCommand = New OleDb.OleDbCommand(sqlString, con)
            Dim dr As OleDb.OleDbDataReader = cmd.ExecuteReader()
            While dr.Read()
                Me.lbl_petname.Text = dr("PetName")

                Me.Image2.ImageUrl = dr("photo")


            End While

            dr.Close()
            con.Close()

        End If
        '   Load_RepliesANDViews()
        Me.Label1.Text = temp


    End Sub



    Protected Sub btn_post_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_post.Click
        petID = Request.QueryString("ID")
        Dim con As New OleDbConnection(ConfigurationManager.ConnectionStrings("PetsConnectionString").ToString())
        Dim qry As String = "INSERT INTO Discussion_tbl (petID,Title,Description,Dateofpost,UserName,UserImage) values (@petID,Title,Description,Dateofpost,User,UserImage)"


        

        con.Open()
        Dim cmd As New OleDbCommand(qry, con)
        cmd.Parameters.AddWithValue("petID", petID)
        cmd.Parameters.AddWithValue("Title", Me.tb_title.Text)
        cmd.Parameters.AddWithValue("Description", Me.tb_description.Text)
        cmd.Parameters.AddWithValue("Dateofpost", Date.Now.ToShortDateString())
        cmd.Parameters.AddWithValue("User", User.Identity.Name)
        cmd.Parameters.AddWithValue("UserImage", GetUserImage())



        cmd.ExecuteNonQuery()
    End Sub

    Function GetUserImage()
        Dim con As New OleDbConnection(ConfigurationManager.ConnectionStrings("PetsConnectionString").ToString())
        con.Open()

        Dim oledbcmd As New OleDbCommand("SELECT * From UserDetails where UserName = '" + User.Identity.Name + "'", con)
        Dim dr As OleDbDataReader = oledbcmd.ExecuteReader()
        dr.Read()
        UserImage = dr("Photo")
        Return UserImage
    End Function

    Function findUSerID()
        Dim con As New OleDbConnection(ConfigurationManager.ConnectionStrings("PetsConnectionString").ToString())
        con.Open()

        Dim oledbcmd As New OleDbCommand("SELECT * From aspnet_Users where UserName = @Username", con)
        oledbcmd.Parameters.AddWithValue("@Username", User.Identity.Name)
        Dim dr As OleDbDataReader = oledbcmd.ExecuteReader()
        dr.Read()
        UserID = dr("UserId")
        Return UserID
    End Function

    Sub Load_RepliesANDViews()

        Dim con As New OleDbConnection(ConfigurationManager.ConnectionStrings("PetsConnectionString").ToString())
        con.Open()

        Dim oledbcmd As New OleDbCommand("SELECT * From DiscussionViewed where ID_fk = " & temp & "", con)

        Dim dr As OleDbDataReader = oledbcmd.ExecuteReader()
        While dr.Read()
            countViews += 1

        End While

        con.Close()
        MsgBox(countViews)



    End Sub


   
End Class